home *** CD-ROM | disk | FTP | other *** search
- Path: gidora.kralizec.net.au!root
- From: jon@zeta.org.au
- Newsgroups: comp.lang.c++
- Subject: Re: locking
- Date: 13 Jan 1996 14:26:47 GMT
- Organization: Kralizec Dialup Internet Sydney, +61-2-837-1183 V.32bis
- Message-ID: <4d8ff7$phs@gidora.kralizec.net.au>
- References: <4d1kl2$64c@daphne.ecmwf.int> <1996Jan12.104534.1741@ittpub>
- Reply-To: jon@zeta.org.au
- NNTP-Posting-Host: dialup08.syd1.zeta.org.au
- X-Newsreader: IBM NewsReader/2 v1.2.5
-
- >
- >Careful here!
-
- Agreed!
-
- >The question boils down to the lifetime of a temporary, in
- >this case the LockObject. When is it destructed? There are differences
- >between compilers currently in use: some compilers destruct the temporary
- >at the end of the block in which it was created (in which case Enno's
- >bracket trick will restrict the lifetime of the lock), but others may
- >destruct the temporary right after the call to its operator->(), in which
- >case the lock is released *before* the `bar()' member function is invoked.
- >I think the draft standard says the temporary will be destructed at the
- >`end of the full expression' (that is, an expression that is not part of
- >some other expression), so Baudoin Raoult's trick should work, but not all
- >compilers are up to date yet.
- >
-
- Admittedly the standard may say something different to the ARM, but the strongest
- assertion the ARM makes about the lifetime of a temporary is
- that it must be destroyed by the end of the scope that created it.
-
- >All in all, it is currently impossible to rely on the lifetime of
- >temporary if you want to write portable code. I would go for an explictly
- >declared instance of the locking object instead - which unfortunately
- >requires extra coding and/or interface replication.
- >
-
- Also agreed, but I don't think it results in extra coding, as I think this example
- from my comp.lang.c++.moderated article shows:
-
- I wrote:
- > f (lockable<foo> fooH) // or perhaps f(lockable<foo> &fooH)
- > {
- > lockable<foo>::guard_t guard(fooH); // lock known by fooH acquired here
- >
- > fooH->bar();
- >
- > // post condition of fooH->bar() is guaranteed here
- > // because lock has not yet been released
- >
- > ...
- >
- > fooH->barbar(); // object accessed again with method pre-condition
- > // guaranteed by client
- >
- > } // lock acquired by guard is guaranteed to be released here
- >
- > lockable<foo>::guard_t is a guard type similar to LockableObject that
- > acquires the lock known by fooH on construction and releases it
- > on deletion.
- >
- > The key thing here is to separate method invocation from
- > lock acquisition. If you only acquire locks around method invocations
- > then you are throwing away valuable knowledge about method
- > post-conditions (and hence the state of the shared object).
- > If you want to maintain that knowledge (and most of the time you do)
- > it is absolutely essential that you are explicit about the period of time
- > that you have exclusive access to the object.
-
-
-
- jon.
-
-